#!/usr/bin/env sh

# 获取操作系统版本号
Get_Dist_Version()
{
  if command -v lsb_release >/dev/null 2>&1; then
    DISTRO_Version=$(lsb_release -rs)
  elif [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    DISTRO_Version="$DISTRIB_RELEASE"
  elif [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO_Version="$VERSION_ID"
  fi

  if [[ "${DISTRO}" = "" || "${DISTRO_Version}" = "" ]]; then
    if command -v python2 >/dev/null 2>&1; then
      DISTRO_Version=$(python2 -c 'import platform; print platform.linux_distribution()[1]')
    elif command -v python3 >/dev/null 2>&1; then
      DISTRO_Version=$(python3 -c 'import platform; print(platform.linux_distribution()[1])')
    else
      Install_LSB
      DISTRO_Version=`lsb_release -rs`
    fi
  fi

  printf -v "${DISTRO}_Version" '%s' "${DISTRO_Version}"
}

# 获取操作系统名及安装命令
Get_Dist_Name()
{
  if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
    DISTRO='CentOS'
    PM='yum'
  elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun Linux" /etc/*-release; then
    DISTRO='Aliyun'
    PM='yum'
  elif grep -Eqi "Amazon Linux" /etc/issue || grep -Eq "Amazon Linux" /etc/*-release; then
    DISTRO='Amazon'
    PM='yum'
  elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
    DISTRO='Fedora'
    PM='yum'
  elif grep -Eqi "Oracle Linux" /etc/issue || grep -Eq "Oracle Linux" /etc/*-release; then
    DISTRO='Oracle'
    PM='yum'
  elif grep -Eqi "Red Hat Enterprise Linux" /etc/issue || grep -Eq "Red Hat Enterprise Linux" /etc/*-release; then
    DISTRO='RHEL'
    PM='yum'
  elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
    DISTRO='Debian'
    PM='apt'
  elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
    DISTRO='Ubuntu'
    PM='apt'
  elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
    DISTRO='Raspbian'
    PM='apt'
  elif grep -Eqi "Deepin" /etc/issue || grep -Eq "Deepin" /etc/*-release; then
    DISTRO='Deepin'
    PM='apt'
  elif grep -Eqi "Mint" /etc/issue || grep -Eq "Mint" /etc/*-release; then
    DISTRO='Mint'
    PM='apt'
  elif grep -Eqi "Kali" /etc/issue || grep -Eq "Kali" /etc/*-release; then
    DISTRO='Kali'
    PM='apt'
  else
    DISTRO='unknow'
  fi
}

Get_RHEL_Version()
{
  Get_Dist_Name
  if [ "${DISTRO}" = "RHEL" ]; then
    if grep -Eqi "release 5." /etc/redhat-release; then
      echo "Current Version: RHEL Ver 5"
      RHEL_Ver='5'
    elif grep -Eqi "release 6." /etc/redhat-release; then
      echo "Current Version: RHEL Ver 6"
      RHEL_Ver='6'
    elif grep -Eqi "release 7." /etc/redhat-release; then
      echo "Current Version: RHEL Ver 7"
      RHEL_Ver='7'
    elif grep -Eqi "release 8." /etc/redhat-release; then
      echo "Current Version: RHEL Ver 8"
      RHEL_Ver='8'
    fi
  fi
}

# 安装 lsb-release, 获取系统版本的相关信息
Install_LSB()
{
  echo "[+] Installing lsb..."
  if [ "$PM" = "yum" ]; then
    yum -y install redhat-lsb
  elif [ "$PM" = "apt" ]; then
    apt-get update
    apt-get --no-install-recommends install -y lsb-release
  fi
}

# 彩色字体
Color_Text()
{
  echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red()
{
  echo $(Color_Text "$1" "31")
}

Echo_Green()
{
  echo $(Color_Text "$1" "32")
}

Echo_Yellow()
{
  echo $(Color_Text "$1" "33")
}

Echo_Blue()
{
  echo $(Color_Text "$1" "34")
}